package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ErpStockTransfer;
import com.ruoyi.system.service.IErpStockTransferService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存调拨Controller
 *
 * @author ruoyi
 * @date 2025-03-07
 */
@RestController
@RequestMapping("/system/transfer")
public class ErpStockTransferController extends BaseController
{
    @Autowired
    private IErpStockTransferService erpStockTransferService;

    /**
     * 查询库存调拨列表
     */
    @PreAuthorize("@ss.hasPermi('system:transfer:list')")
    @GetMapping("/list")
    public TableDataInfo list(ErpStockTransfer erpStockTransfer)
    {
        startPage();
        List<ErpStockTransfer> list = erpStockTransferService.selectErpStockTransferList(erpStockTransfer);
        return getDataTable(list);
    }

    /**
     * 导出库存调拨列表
     */
    @PreAuthorize("@ss.hasPermi('system:transfer:export')")
    @Log(title = "库存调拨", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpStockTransfer erpStockTransfer)
    {
        List<ErpStockTransfer> list = erpStockTransferService.selectErpStockTransferList(erpStockTransfer);
        ExcelUtil<ErpStockTransfer> util = new ExcelUtil<ErpStockTransfer>(ErpStockTransfer.class);
        util.exportExcel(response, list, "库存调拨数据");
    }

    /**
     * 获取库存调拨详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:transfer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(erpStockTransferService.selectErpStockTransferById(id));
    }

    /**
     * 新增库存调拨
     */
    @PreAuthorize("@ss.hasPermi('system:transfer:add')")
    @Log(title = "库存调拨", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpStockTransfer erpStockTransfer)
    {
        return toAjax(erpStockTransferService.insertErpStockTransfer(erpStockTransfer));
    }

    /**
     * 修改库存调拨
     */
    @PreAuthorize("@ss.hasPermi('system:transfer:edit')")
    @Log(title = "库存调拨", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpStockTransfer erpStockTransfer)
    {
        return toAjax(erpStockTransferService.updateErpStockTransfer(erpStockTransfer));
    }

    /**
     * 删除库存调拨
     */
    @PreAuthorize("@ss.hasPermi('system:transfer:remove')")
    @Log(title = "库存调拨", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(erpStockTransferService.deleteErpStockTransferByIds(ids));
    }

    /**
     * 提交库存调拨
     */
    @PreAuthorize("@ss.hasPermi('system:transfer:edit')")
    @Log(title = "库存调拨", businessType = BusinessType.UPDATE)
    @PutMapping("/submit/{id}")
    public AjaxResult submitTransfer(@PathVariable Long id) {
        return toAjax(erpStockTransferService.submitTransfer(id));
    }

    /**
     * 撤回调拨
     */
    @PreAuthorize("@ss.hasPermi('system:transfer:edit')")
    @Log(title = "库存调拨", businessType = BusinessType.UPDATE)
    @PutMapping("/withdraw/{id}")
    public AjaxResult withdrawTransfer(@PathVariable Long id) {
        return toAjax(erpStockTransferService.withdrawTransfer(id));
    }

    /**
     * 确认调拨
     */
    @PreAuthorize("@ss.hasPermi('system:transfer:edit')")
    @Log(title = "库存调拨", businessType = BusinessType.UPDATE)
    @PutMapping("/confirm/{id}")
    public AjaxResult confirmTransfer(@PathVariable Long id) {
        return toAjax(erpStockTransferService.confirmTransfer(id));
    }

    /**
     * 根据仓库ID获取商品列表
     * @param warehouseId 仓库ID
     * @return 商品列表
     */
    @GetMapping("/goods/{warehouseId}")
    public TableDataInfo listGoodsByWarehouse(@PathVariable Long warehouseId) {
        startPage();
        List<Map<String, Object>> list = erpStockTransferService.selectGoodsByWarehouse(warehouseId);
        return getDataTable(list);
    }
}
